package com.baofu.paycore.manager.storage.impl;

import com.baofu.paycore.common.exception.ManagerErrorCode;
import com.baofu.paycore.common.exception.PayCoreBizException;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.baofu.paycore.common.exception.PayCoreManagerException;
import com.baofu.paycore.dal.mapper.SubRefundMapper;
import com.baofu.paycore.dal.model.SubRefundDO;
import com.baofu.paycore.manager.storage.Storage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 退款资金单处理Manager
 * <p>
 * </p>
 * User: kwl Date: 2016/6/4 ProjectName: paycore Version: 5.0.0
 */
@Slf4j
@Service
public class SubRefundManager implements Storage<SubRefundDO> {

    /**
     * 退款内部资金单Mapper
     */
    @Autowired
    private SubRefundMapper subRefundMapper;

    /**
     * 新增退款资金单明细
     *
     * @param subRefundDO    退款资金单明细信息
     */
    @Override
    public int addOrder(SubRefundDO subRefundDO) {

        log.debug("新增退款资金单明细, parameter:{}", subRefundDO);
        int insertRows = subRefundMapper.insert(subRefundDO);
        if(1 != insertRows){
            log.error("新增退款资金单明细失败, error:{}", PayCoreErrorCode.INSERT_DATA_NOT_NOE);
            throw new PayCoreManagerException(PayCoreErrorCode.INSERT_DATA_NOT_NOE);
        }
        return insertRows;
    }

    /**
     * 更新退款资金单明细
     * @param subRefundDO    退款资金单明细
     */
    @Override
    public int modifyOrder(SubRefundDO subRefundDO) {

        log.debug("更新退款资金单明细, parameter:{}", subRefundDO);
        int updateRows = subRefundMapper.update(subRefundDO);
        if(1 != updateRows){
            log.error("更新退款资金单明细, error:{}", PayCoreErrorCode.UPDATE_DATA_NOT_ONE);
            throw new PayCoreManagerException(PayCoreErrorCode.UPDATE_DATA_NOT_ONE);
        }
        return updateRows;
    }

    @Override
    public int deleteOrder(SubRefundDO subRefundDO) {
        return 0;
    }

    /**
     * 根据资金单单号查询资金单信息
     *
     * @param fundDetailNo    资金单号
     */
    public SubRefundDO queryByFundDetailNo(String fundDetailNo){

        SubRefundDO subRefundDO = subRefundMapper.selectByFundDetailNo(fundDetailNo);
        if(null == subRefundDO){
            log.error("查询支付主单信息为空, fundDetailNo:{},error:{}",fundDetailNo,
                    PayCoreErrorCode.SUB_ORDER_IS_NOT_EXISTED);
            throw new PayCoreBizException(PayCoreErrorCode.SUB_ORDER_IS_NOT_EXISTED);
        }

        return subRefundDO;
    }

}
